package com.cc.secondKill.commnons;

import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import redis.clients.util.Hashing;
import redis.clients.util.Sharded;

import java.util.ArrayList;

public class ShardedJedisPools {
    private static String redisHost = PropertiesUtils.getByKey("redis.host");
    private static String redisPort = PropertiesUtils.getByKey("redis.port");
    private static ShardedJedisPool pool = null;
    private static ArrayList<JedisShardInfo> infoArrayList = new ArrayList<JedisShardInfo>();

    private static void init(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMinIdle(10);
        config.setMaxTotal(20);
        config.setMaxIdle(20);
        config.setTestOnBorrow(true);
        config.setBlockWhenExhausted(true);
        config.setTestOnReturn(false);
        JedisShardInfo info = new JedisShardInfo(redisHost,redisPort);
        infoArrayList.add(info);
        pool = new ShardedJedisPool(config,infoArrayList, Hashing.MURMUR_HASH, Sharded.DEFAULT_KEY_TAG_PATTERN);
    }

    static {
        init();
    }

    public static ShardedJedis getResource(){
        return pool.getResource();
    }

    @Deprecated
    public static void returnResource(ShardedJedis jedis){
        pool.returnResource(jedis);
    }

    @Deprecated
    public static void returnBrokenResource(ShardedJedis jedis){
        pool.returnBrokenResource(jedis);
    }

}
